package com.vegetable.modules.service.bank.convert;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.vegetable.modules.entity.finance.Transactions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 工行流水标准化字段输出
 */
@Slf4j
public class ICBCConverter extends BankGenerator{
    private static final String bankMark = "ICBC";
    public List<Transactions> convert(MultipartFile file) throws Exception {
        List<Transactions> result = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            Sheet sheet = workbook.getSheetAt(0);
            int lastRow = sheet.getLastRowNum();
            for(int i = 2; i <= lastRow; i++){
                Row currentR = sheet.getRow(i);
                if(StrUtil.isBlank(getStringCell(currentR, 0))){
                    continue;
                }
                Transactions thisT = new Transactions();
                // 借出 贷入
                thisT.setTradingType("借".equals(getStringCell(currentR,3).replace("\t", "")) ? "EXPENSE" : "INCOME");
                thisT.setTradingTime(DateUtil.parse(getStringCell(currentR, 2), "yyyy-MM-dd HH:mm:ss"));
                thisT.setTradingAmount(getBCCell(currentR,11));
                thisT.setBalanceAmount(getBCCell(currentR,13));
                thisT.setBankName("");
                thisT.setBankNo(getStringCell(currentR,1));
                thisT.setCounterparty(getStringCell(currentR,4));
                thisT.setTradingDescription(getStringCell(currentR,7));
                thisT.setRemark(getStringCell(currentR,8));
                thisT.setChannel(bankMark);
                thisT.setTradingNo(Transactions.generateUniqueFlowId(thisT));
                result.add(thisT);
            }
        }catch (Exception e){
            log.error("转换工行流水发生异常--->", e);
            return null;
        }
        return result;
    }
}
